/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.model.entity;

import java.util.List;

import org.newdawn.slick.geom.Rectangle;

import ca.usherbrooke.model.BaseModel;
import ca.usherbrooke.model.bridge.IEntityModelObject;
import ca.usherbrooke.model.entity.behaviours.EquipmentBehaviour;
import ca.usherbrooke.model.entity.behaviours.FightBehaviour;
import ca.usherbrooke.model.entity.behaviours.JumpBehaviour;
import ca.usherbrooke.model.entity.behaviours.VisionBehaviour;
import ca.usherbrooke.model.equipment.EquipmentModel;
import ca.usherbrooke.model.equipment.ProtectionModel;
import ca.usherbrooke.model.equipment.WeaponModel;
import ca.usherbrooke.model.feature.GroundModel;
import ca.usherbrooke.tools.position.Vector2D;

/**
 * @author Cody Stoutenburg
 * 
 */
public class EntityModel extends BaseModel implements IEntityGame {
	private static final long serialVersionUID = 1L;

	// Move information
	private Vector2D targetPosition;
	private Vector2D lastTargetPosition;
	// the speed is in pixel per second
	private Float maxSpeed;
	private Vector2D currentSpeed;

	// Vision information
	private GroundModel walkOn;

	private final EquipmentBehaviour equipmentBehaviour;
	private final JumpBehaviour jumpBehaviour;
	private final FightBehaviour fightBehaviour;
	private final VisionBehaviour visionBehaviour;

	public EntityModel(IEntityModelObject modelObject) {
		super(modelObject.getName(), new Rectangle(modelObject.getPositionX(),
				modelObject.getPositionY(), modelObject.getPositionWidth(),
				modelObject.getPositionHeight()));

		// all Entity behaviour
		equipmentBehaviour = new EquipmentBehaviour(this,
				modelObject.getMaxCapacity());
		fightBehaviour = new FightBehaviour(this, modelObject.getMaxHp(),
				modelObject.getCurrentHp(),
				modelObject.getAttackReloadTimeMs(),
				modelObject.getCurrentAttackModifier(),
				modelObject.getDamageAdd());

		jumpBehaviour = new JumpBehaviour(this,
				modelObject.getJumpReloadTimeMs(),
				modelObject.getMaxJumpDurationInMs());

		visionBehaviour = new VisionBehaviour(this,
				modelObject.getVisionWidth(), modelObject.getVisionHeight());

		maxSpeed = (float) Math.pow(modelObject.getMaxSpeedValue(), 2);
		currentSpeed = new Vector2D(modelObject.getCurrentSpeedX(),
				modelObject.getCurrentSpeedY());
	}

	@Override
	public Rectangle getViewingArea() {
		return this.visionBehaviour.getViewingArea();
	}

	@Override
	public Integer getMaxHP() {
		return this.fightBehaviour.getMaxHP();
	}

	@Override
	public Integer getCurrentHP() {
		return this.fightBehaviour.getCurrentHP();
	}

	@Override
	public Float getMaxSpeed() {
		return Double.valueOf(Math.sqrt(this.maxSpeed)).floatValue();
	}

	public void setMaxSpeed(double maxSpeed) {
		this.maxSpeed = (float) Math.pow(maxSpeed, 2);
	}

	@Override
	public GroundModel getWalkOn() {
		return this.walkOn;
	}

	@Override
	public Vector2D getRealSpeed() {
		Vector2D tempSpeed = null;
		if (isJumping()) {
			tempSpeed = currentSpeed;
		} else {
			tempSpeed = currentSpeed.multiply(walkOn.getSpeedFactor());
		}
		return tempSpeed;

	}

	@Override
	public Vector2D getCurrentSpeed() {
		return this.currentSpeed;
	}

	@Override
	public void setSpeed(Vector2D newSpeed) {

		if (maxSpeed - newSpeed.getSquaredMagnitude() > -0.01) {
			if (isJumping()) {
				// to avoid increase speed on jump state and change direction
				if (newSpeed.getSquaredMagnitude() < currentSpeed
						.getSquaredMagnitude()) {
					if (newSpeed.getAt().equals(currentSpeed.getAt())) {
						this.currentSpeed = newSpeed;
					}
				}
				if (newSpeed.getSquaredMagnitude() == 0) {
					jumpBehaviour.stopJump();
				}
			} else {
				this.currentSpeed = newSpeed;
				if (currentSpeed.equals(Vector2D.NULL_VECTOR)) {
					this.targetPosition = null;
				}
			}
		}
	}

	@Override
	public void setTargetPosition(Vector2D upperLeftCorner) {
		targetPosition = upperLeftCorner;
	}

	@Override
	public Vector2D getTargetPosition() {
		return targetPosition;
	}

	@Override
	public void setWalkOn(GroundModel model) {
		this.walkOn = model;
	}

	@Override
	public Integer getRemainingAttackTime() {
		return fightBehaviour.getRemainingAttackTime();
	}

	@Override
	public void doAttack(IEntityModel defender) {
		fightBehaviour.doAttack(defender);
	}

	@Override
	public void doLogic(int delta) {
		// reset at beginning
		fightBehaviour.doLogic(delta);
		jumpBehaviour.doLogic(delta);

		if (targetPosition != null) {
			float deltaInSec = delta / 1000.0f;
			Vector2D move = getRealSpeed();

			move = move.multiply(deltaInSec);

			Float distance = targetPosition.getSquaredDistance(getPosition());
			if (distance < 10f) {
				this.setSpeed(Vector2D.NULL_VECTOR);
				this.lastTargetPosition = targetPosition;
				this.targetPosition = null;
			} else if (targetPosition.getSquaredDistance(getPosition()) <= move
					.getSquaredMagnitude()) {
				float x = this.size.getX();
				float y = this.size.getY();

				Vector2D speed = new Vector2D((targetPosition.getX() - x)
						/ deltaInSec, (targetPosition.getY() - y) / deltaInSec);

				this.setSpeed(speed);
			}
		}
	}

	/**
	 * move the entity from the time delta
	 * 
	 * @param delta
	 *            time in ms
	 */
	@Override
	public void doMove(int delta) {
		float deltaInSec = delta / 1000.0f;
		Vector2D move = getRealSpeed();

		move = move.multiply(deltaInSec);
		float x = this.size.getX();
		float y = this.size.getY();

		x += move.getX().floatValue();
		y += move.getY().floatValue();

		this.size.setX(x);
		this.size.setY(y);
	}

	@Override
	public List<EquipmentModel> getEquipments() {
		return this.equipmentBehaviour.getEquipments();
	}

	@Override
	public void doDamage(Integer damage) {
		fightBehaviour.doDamage(damage);
	}

	public void doHeal(int value) {
		fightBehaviour.doHeal(value);
	}

	@Override
	public Float getRange() {
		return this.fightBehaviour.getRange();
	}

	@Override
	public Integer getAttackModifier() {
		return this.fightBehaviour.getAttackModifier();
	}

	@Override
	public Integer getDefense() {
		return this.fightBehaviour.getDefense();
	}

	// Jump Method
	@Override
	public void doJump() {
		jumpBehaviour.jump();
	}

	@Override
	public Boolean isJumping() {
		return jumpBehaviour.isJumping();
	}

	@Override
	public Integer getCurrentJumpDuration() {
		return jumpBehaviour.getCurrentJumpDuration();
	}

	@Override
	public Integer getMaxJumpDuration() {
		return jumpBehaviour.getMaxJumpDuration();
	}

	@Override
	public Integer getRemaningJumpReload() {
		return jumpBehaviour.getRemaningJumpReload();
	}

	@Override
	public Integer getJumpReloadTime() {
		return jumpBehaviour.getJumpReloadTime();
	}

	public boolean isHoldEquipements() {
		return equipmentBehaviour.isHoldEquipements();
	}

	@Override
	public Boolean canPickUpEquipment(EquipmentModel newEquipement) {
		return equipmentBehaviour.canPickUpEquipment(newEquipement);
	}

	@Override
	public void pickUp(EquipmentModel newEquipement) {
		equipmentBehaviour.pickUp(newEquipement);
	}

	@Override
	public Boolean putDown(EquipmentModel oldEquipement) {
		return equipmentBehaviour.putDown(oldEquipement);
	}

	@Override
	public Vector2D getLastTargetReach() {
		return lastTargetPosition;
	}

	@Override
	public Boolean isDead() {
		return fightBehaviour.isDead();
	}

	@Override
	public Integer getCapacity() {
		return equipmentBehaviour.getCapacity();
	}

	@Override
	public Integer getMaxCapacity() {
		return equipmentBehaviour.getMaxCapacity();
	}

	@Override
	public Boolean canWearProtection(ProtectionModel newProtection) {
		return FightBehaviour.canWearProtection(this, newProtection);
	}

	@Override
	public Boolean doesWearProtection(ProtectionModel protection) {
		return FightBehaviour.doesWearProtection(this, protection);
	}

	@Override
	public void takeProtection(ProtectionModel protection) {
		this.fightBehaviour.takeProtection(protection);
	}

	@Override
	public void putDownProtection(ProtectionModel protection) {
		this.fightBehaviour.putDownProtection(protection);
	}

	@Override
	public Boolean canTakeWeapon(WeaponModel weapon) {
		return FightBehaviour.canTakeWeapon(this, weapon);
	}

	@Override
	public void takeWeapon(WeaponModel weapon) {
		this.fightBehaviour.takeWeapon(weapon);
	}

	@Override
	public void putDownWeapon() {
		this.fightBehaviour.putDownWeapon();
	}

	@Override
	public List<WeaponModel> getWeaponsInEquipments() {
		return this.equipmentBehaviour.getWeapons();
	}

	@Override
	public List<ProtectionModel> getProtectionsInEquipments() {
		return this.equipmentBehaviour.getProtections();
	}

	@Override
	public WeaponModel getCurrentWeapon() {
		return this.fightBehaviour.getCurrentWeapon();
	}

	@Override
	public List<ProtectionModel> getCurrentProtection() {
		return this.fightBehaviour.getCurrentProtection();
	}

}
